package com.sam.demo.redis;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import com.google.common.collect.Lists;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisDevConfiguration {

	@Bean(name = "stringRedisDevTemplate")
	public StringRedisTemplate stringRedisTemplate(@Value("${spring.redis.redis-dev.host}") String hostName,
			@Value("${spring.redis.redis-dev.port}") int port,
			@Value("${spring.redis.redis-dev.password}") String password,
			@Value("${spring.redis.redis-dev.pool.max-idle}") int maxIdle,
			@Value("${spring.redis.redis-dev.pool.max-active}") int maxTotal,
			@Value("${spring.redis.redis-dev.database}") int index,
			@Value("${spring.redis.redis-dev.pool.max-wait}") long maxWaitMillis) {
		StringRedisTemplate temple = new StringRedisTemplate();
		temple.setConnectionFactory(
				connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis));
		return temple;
	}

	@SuppressWarnings("rawtypes")
	@Bean(name = "redisDevTemplate")
	public RedisTemplate redisTemplate(@Value("${spring.redis.redis-dev.host}") String hostName,
			@Value("${spring.redis.redis-dev.port}") int port,
			@Value("${spring.redis.redis-dev.password}") String password,
			@Value("${spring.redis.redis-dev.pool.max-idle}") int maxIdle,
			@Value("${spring.redis.redis-dev.pool.max-active}") int maxTotal,
			@Value("${spring.redis.redis-dev.database}") int index,
			@Value("${spring.redis.redis-dev.pool.max-wait}") long maxWaitMillis) {
		RedisTemplate temple = new RedisTemplate();
		temple.setConnectionFactory(
				connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis));
		return temple;
	}

	public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
			int maxTotal, int index, long maxWaitMillis) {
		
		RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Lists.newArrayList("192.168.1.107:6379"));
		
		JedisConnectionFactory jedis = new JedisConnectionFactory();
		jedis.setHostName(hostName);
		jedis.setPort(port);
		if (!StringUtils.isEmpty(password)) {
			jedis.setPassword(password);
		}
		if (index != 0) {
			jedis.setDatabase(index);
		}
		
		JedisPoolConfig poolCofig = new JedisPoolConfig();
		poolCofig.setMaxIdle(maxIdle);
		poolCofig.setMaxTotal(maxTotal);
		poolCofig.setMaxWaitMillis(maxWaitMillis);
		
		jedis.setPoolConfig(poolCofig);
		// 初始化连接pool
		jedis.afterPropertiesSet();
		RedisConnectionFactory factory = jedis;

		return factory;
	}
//
//	public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
//		JedisPoolConfig poolCofig = new JedisPoolConfig();
//		poolCofig.setMaxIdle(maxIdle);
//		poolCofig.setMaxTotal(maxTotal);
//		poolCofig.setMaxWaitMillis(maxWaitMillis);
//		poolCofig.setTestOnBorrow(testOnBorrow);
//		return poolCofig;
//	}
	
//	@Bean
//    public RedisConnectionFactory connectionFactory() {
//		RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Lists.newArrayList("192.168.1.107:6379"));
//		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration);
//		jedisConnectionFactory.setPassword("");
//        return jedisConnectionFactory;
//    }
}
